package com.ctyon.watchmanager.ui.fragment;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import com.ctyon.common.util.DataUtil;
import com.ctyon.common.util.TSnackbarUtil;
import com.ctyon.netlibrary.net.revert.BaseResponseEntity;
import com.ctyon.netlibrary.use.BaseObserver;
import com.ctyon.watchmanager.BasicApp;
import com.ctyon.watchmanager.R;
import com.ctyon.watchmanager.bean.DisturbBean;
import com.ctyon.watchmanager.databinding.FragmentDisableBinding;
import com.ctyon.watchmanager.dialog.PickerDialog;
import com.ctyon.watchmanager.model.net.service.NetService;
import com.ctyon.watchmanager.present.DisablePresenter;
import com.ctyon.watchmanager.ui.BaseFragment;
import com.orhanobut.logger.Logger;

import java.util.concurrent.TimeUnit;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * CreateDate：19-6-25 on 上午9:14
 * Describe:
 * Coder: lee
 */
public class DisableFragment extends BaseFragment {

    public static final String TAG = "DisableFragment";

    private FragmentDisableBinding mBinding;
    private PickerDialog dialog;
    private final String[] disableArr = new String[3];
    private int disableIndex;
    private String disable;
    private DisturbBean disturbBean;

    public static DisableFragment newInstance() {
        final DisableFragment fragment = new DisableFragment();
        final Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_disable, container, false);
        mBinding.setDisablePresenter(disablePresenter);

        return mBinding.getRoot();

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        disable = BasicApp.getInstance().getWatch().getDisturb();
        disturbBean = new DisturbBean();
        if (!DataUtil.isEmptyAnd(disable) && disable.contains("*")){
            String[] strings = disable.split("&");
            for (int i = 0; i < strings.length; i++) {
                disableArr[i] = strings[i];
            }
            mBinding.setDisturbBean(disturbBean.setDisturb(disableArr));
        }
    }

    private final DisablePresenter disablePresenter = new DisablePresenter() {
        @Override
        public void onSaveBtClick() {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < disableArr.length; i++) {
                if (DataUtil.isEmptyOr(disableArr[i]) || disableArr[i].equals("null")){
                    disableArr[i] = "00*00-00*00";
                }
                if (builder.length() > 0){
                    builder.append("&");
                }
                builder.append(disableArr[i]);

            }
            String disableData =  builder.toString();
            Logger.t(LTAG).d("disableData : " + disableData);
            if (!DataUtil.isEmptyAnd(BasicApp.getInstance().getWatch().getDisturb()) && disableData.contains(BasicApp.getInstance().getWatch().getDisturb())){
                TSnackbarUtil.TSBError(getActivity(), "无修改");
                return;
            }
            final String method = "noDisturb";
            if (NetService.getInstance().isDisable(method)){
                TSnackbarUtil.TSBError(getActivity(), "请稍候...");
                return;
            }
            NetService.getInstance()
                    .method(method)
                    .noDisturb(BasicApp.getInstance().getManager().getManagerId(), BasicApp.getInstance().getWatch().getMeid(), disableData)
                    .timeout(10, TimeUnit.SECONDS)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<BaseResponseEntity>() {
                        @Override
                        public void onSuccess(BaseResponseEntity responseEntity) {
                            Logger.d("onSuccess : " + responseEntity.toString());
                            TSnackbarUtil.TSBSuccess(getActivity(), "操作成功");
                            BasicApp.getInstance().getWatch().setDisturb(disableData);
                        }

                        @Override
                        public String onOver() {
                            return method;
                        }
                    });
        }

        @Override
        public void onTimeClick(int index) {
            Logger.t(LTAG).i("onTimeClick : " + index);
            disableIndex = index;
            showDialog();
        }
    };

    private final PickerDialog.OnPickerCallback dialogClickListener = (hourB, minuteB, hourE, minuteE) -> {
        if ((hourB + minuteB + hourE + minuteE).equals("00000000")){
            disableArr[disableIndex] = "";
        } else {
            StringBuilder disableBuilder = new StringBuilder();
            disableBuilder.append(hourB);
            disableBuilder.append("*");
            disableBuilder.append(minuteB);
            disableBuilder.append("-");
            disableBuilder.append(hourE);
            disableBuilder.append("*");
            disableBuilder.append(minuteE);
            String time = disableBuilder.toString();
            Logger.t(LTAG).i("onTimeData : " + time);
            disableArr[disableIndex] = time;
        }
        mBinding.setDisturbBean(disturbBean.setDisturb(disableArr));
    };

    private void showDialog() {
        if (null == dialog){
            Logger.t(LTAG).i("Create Dialog");
            dialog = new PickerDialog(getActivity(), R.style.CustomDialog);
            dialog.setListener(dialogClickListener);
            dialog.setCancelable(false);
            Window window = dialog.getWindow();
            window.setGravity(Gravity.CENTER);
            window.setWindowAnimations(R.style.CustomDialogStyle);
        }
        dialog.show();
    }

}
